package com.ctgu.rabbitmq.rabbitmq.exchange.fanoutExchange;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.FanoutExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @ ClassName FanoutExchangeConfig
 * @ Author Alex2
 * @ Date 2025/8/14 14:16
 **/
@Configuration
public class FanoutExchangeConfig {
    public static final String FANOUT_EXCHANGE = "fanout_exchange";
    public static final String QUEUE_A = "queue_a";
    public static final String QUEUE_B = "queue_b";

    // 创建扇形交换机
    @Bean(name = FANOUT_EXCHANGE)
    public FanoutExchange fanoutExchange() {
        return new FanoutExchange(FANOUT_EXCHANGE);
    }

    // 创建队列
    @Bean(name = QUEUE_A)
    public Queue queueA() {
        return new Queue(QUEUE_A, true);
    }

    @Bean(name = QUEUE_B)
    public Queue queueB() {
        return new Queue(QUEUE_B, true);
    }

    // 绑定交换机和队列
    @Bean
    public Binding bindingQueueA(
            @Qualifier(QUEUE_A) Queue queue,
            @Qualifier(FANOUT_EXCHANGE) FanoutExchange fanoutExchange) {
        return BindingBuilder.bind(queue).to(fanoutExchange);
    }

    @Bean
    public Binding bindingQueueB(
            @Qualifier(QUEUE_B) Queue queue,
            @Qualifier(FANOUT_EXCHANGE) FanoutExchange fanoutExchange) {
        return BindingBuilder.bind(queue).to(fanoutExchange);
    }
}